import EventCenter from "../../lib/EventCenter";
import Sdk from "../../lib/Sdk";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI";
import EventEunm from "../data/EventEunm";
import PlayerData from "../data/PlayerData";
import RankManager from "../RankManager";
export default class ChangeName extends ui.ChangeNameUI{
    onEnable(){
        this.numTxt.text="x"+PlayerData.getIns().getItemNum("1000001");
        if(PlayerData.getIns().name){
            this.nameTxt.text=PlayerData.getIns().name;
        }
        this.okBtn.on(Laya.Event.CLICK,this,this.okClick);
        this.videoBtn.on(Laya.Event.CLICK,this,this.videoClick);
        this.closeBtn.on(Laya.Event.CLICK,this,this.close);
        if(PlayerData.getIns().getItemNum("1000001")<=0)this.videoBtn.getChildByName("tip")["text"]="分享游戏";
    }

    private okClick(){
        if(!this.nameTxt.text)
        {
            TipManager.ins.showMsgAlert("不能为空",this);
            return;
        }
        if(PlayerData.getIns().getItemNum("1000001")>0){
            this.numTxt.text="x"+PlayerData.getIns().getItemNum("1000001");
            PlayerData.getIns().addItem("1000001",-1);
            PlayerData.getIns().setName(this.nameTxt.text);
            TipManager.ins.showMsgAlert("改名成功");
            RankManager.getIns().updateName();
            EventCenter.getIns().event(EventEunm.changeName);
            this.close();
        }else{
            TipManager.ins.showMsgAlert("没有改名卡",this);
        }
    }

    private videoClick(){
        if(PlayerData.getIns().getItemNum("1000001")<=0){
            Sdk.getIns().share();
            Laya.timer.once(1000,this,()=>{
                PlayerData.getIns().addItem("1000001",1);
                this.numTxt.text="x"+PlayerData.getIns().getItemNum("1000001");
                this.videoBtn.getChildByName("tip")["text"]="观看广告";
            });
        }else{

            Sdk.getIns().showAd((_code:number)=>{
                if(_code==0){
                    PlayerData.getIns().addItem("1000001",1);
                    this.numTxt.text="x"+PlayerData.getIns().getItemNum("1000001");
                }else{
                    TipManager.ins.showMsgAlert("观看完整视频才能免费获得",this);
                }
            });
        }
    }

    onDestroy(){
        this.okBtn.off(Laya.Event.CLICK,this,this.okClick);
        this.videoBtn.off(Laya.Event.CLICK,this,this.videoClick);
        this.closeBtn.off(Laya.Event.CLICK,this,this.close);
    }
}